﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace VirtualCamUI.Services
{
    class ImageFileService : IImageSourceService
    {
        private string _sourcedir;
        string[] _imagefiles;
        int index = -1;

        public string SourceUri
        {
            get
            {
                return _sourcedir;
            }
            set
            {
                _sourcedir = value;
                index = -1;
                BuildImageArray();
            }
        }

        private void BuildImageArray()
        {
           _imagefiles = Directory.GetFiles(_sourcedir, "*.jpg");
        }

        public System.Windows.Media.ImageSource GetNextImage()
        {
            if (index >= _imagefiles.Length -1) index = 0;
            else index++;

            MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(_sourcedir, _imagefiles[index])));

            BitmapImage bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.DecodePixelHeight = 200;
            bmp.DecodePixelWidth = 300;
            bmp.StreamSource = ms;
            bmp.UriSource = new Uri(Path.Combine(_sourcedir, _imagefiles[index]), UriKind.Absolute);
            bmp.EndInit();


            return bmp;
        }
    }
}
